package org.example.Frame;

import org.example.Util.FileUtil;
import org.example.Pojo.User;
import org.example.implement.MyFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Objects;

public class login extends JFrame implements MyFrame {
    //变量
    private JButton loginButton;
    private JButton registerButton;
    private JLabel userLabel ;
    private JTextField userText;
    private JLabel passwordLabel;
    private JPasswordField passwordText;
    private register register;
    private Map<String ,String> map;//用户查找是否登录成功

    //构造函数
    public login() throws HeadlessException {
        //初始化界面
        this.setTitle("登录界面");
        this.setSize(400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //利用绘画板进行组件初始化
        JPanel p=new JPanel();
        draw(p);
        this.add(p);
        this.setVisible(true);
        //初始化数据
        map=new FileUtil().Find();
    }

    //关闭方法
    @Override
    public void close(){
        this.setVisible(false);
    }
    //绘画板
    @Override
    public void draw(JPanel p){
        p.setLayout(null);//不需要布局管理器

        userLabel = new JLabel("用户名：");//用户名标签
        userLabel.setBounds(30, 50, 80, 25);
        p.add(userLabel);

        userText = new JTextField(20);//用户名填写
        userText.setBounds(120, 50, 165, 25);
        p.add(userText);

        passwordLabel = new JLabel("密码：");//密码标签
        passwordLabel.setBounds(30, 80, 80, 25);
        p.add(passwordLabel);

        passwordText = new JPasswordField(20);//密码填写
        passwordText.setBounds(120, 80, 165, 25);
        p.add(passwordText);

        loginButton = new JButton("登录");//登录按钮
        loginButton.setBounds(40, 140, 80, 25);
        p.add(loginButton);

        //登录按钮响应
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String password="";//密码的临时变量
                for(char a:passwordText.getPassword()){
                    password +=a;
                }//对密码进行获取

                //两种无法登录的情况
                if(!map.containsKey(userText.getText())){
                    JOptionPane.showMessageDialog(new JOptionPane(),"用户不存在","警告",JOptionPane.WARNING_MESSAGE);
                }
                else if (!Objects.equals(map.get(userText.getText()),password)) {
                    JOptionPane.showMessageDialog(new JOptionPane(),"密码不正确","警告",JOptionPane.WARNING_MESSAGE);
                }
                //登录成功后切换界面
                else{
                    System.out.println("成功登录");
                    User player=new User(userText.getText(),password);
                    new Difficulty(player);
                    close();
                }
            }
        });

        registerButton = new JButton("注册");//注册按钮
        registerButton.setBounds(200, 140, 80, 25);
        p.add(registerButton);
        //注册按钮重写
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                register=new register(map);
                close();
            }
        });
    }
}
